const config = require('../../config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addresses:[],
    swiperHeight: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var userInfo = getApp().globalData.userInfo;
      var that =this;
        wx.request({
          url: config.requestUrl + '/accountAddress/findAddress',
          header:{"token":userInfo.xOpenid},
          success(res){
            var addresses = res.data.data;
            that.setData({"addresses":addresses});
          }
        });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          swiperHeight: res.windowHeight - 60
        });
      }
    })
        
  },
  deleteAddress:function(e){
    var that =this;
    var userInfo = getApp().globalData.userInfo;
    var addressId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除该地址',
      success(res) {
        if (res.confirm) {
           wx.request({
             url: config.requestUrl + '/accountAddress/delete',
             header: { "token": userInfo.xOpenid },
             data: { id:addressId},
             success(res){
               var addresses = res.data.data;
               console.log(addresses);
               that.setData({ "addresses": addresses});
             }
           })
        } 
       
      }
    })
  },
  chooseAddress:function(e){
    var userInfo = getApp().globalData.userInfo;
    var addressId = e.currentTarget.dataset.id;
       wx.request({
         url: config.requestUrl + '/accountAddress/chooseAddress',
         header: { "token": userInfo.xOpenid },
         data: { id: addressId,status:1},
         success(res){
           wx.redirectTo({
             url: '/pages/order/order',
           })
         }
       })
  },
  toNewAddress: function () {
    wx.redirectTo({
      url: '/pages/newAddress/newAddress',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})